#################################### Appendix (E) ##############################################
################################################################################################

# Load the required packages
rm(list = ls())
library(fixest)
library(readstata13)


#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 
# data_3rounds: Contains data on the 2 rounds of the election, including the pre-electoral period as an additional round

data <- read_dta("data_2rounds.dta")
data3 <- read_dta("data_3rounds.dta")

#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")




######## Figure (2) 

#Panel (A): Total violence

main1 <- fenegbin(allintimid ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(allintimid ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>75))

main3 <- fenegbin(allintimid ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>80))

main4 <- fenegbin(allintimid ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>90))



fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("Full Sample", "Urban (>75%)", "Urban (>80%)", "Urban (>90%)"), title = "Model")


#Panel (B): Violence against voters

main1 <- fenegbin(voter_total ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(voter_total ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>75))

main3 <- fenegbin(voter_total ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>80))

main4 <- fenegbin(voter_total ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>90))



fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("Full Sample", "Urban (>75%)", "Urban (>80%)", "Urban (>90%)"), title = "Model")


# Panel (C): Violence by state actors 

main1 <- fenegbin(state_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(state_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>75))

main3 <- fenegbin(state_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>80))

main4 <- fenegbin(state_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>90))


fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("Full Sample", "Urban (>75%)", "Urban (>80%)", "Urban (>90%)"), title = "Model")


#Panel (D): Violence by non-state actors 

main1 <- fenegbin(nonstate_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(nonstate_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>75))

main3 <- fenegbin(nonstate_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>80))

main4 <- fenegbin(nonstate_intimidation ~  newndp + mb_dummy +split_ndp + pct_emp + sdeduscore+ pct_urban  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, subset(data, data$pct_urban>90))


fixest::coefplot(list(main1,main2, main3, main4), 
                 keep=c("MB Running", "New NDP", "NDP Dissidents", "Urban", "Employment", "Education", "Protest"), 
                 dict=c("reset", "mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
                        "sdeduscore"="Education (sd)","newndp"="New NDP", "split_ndp"="NDP Dissidents", "logprotest"="Protest"), 
                 lab.fit="multi", horiz=T, main="") 

legend("bottomright", col = 1:4, lwd = 1, pch=c(16,17,15,13),
       legend = c("Full Sample", "Urban (>75%)", "Urban (>80%)", "Urban (>90%)"), title = "Model")


###### Table (10)

main1 <- fenegbin(voter_total ~ candidate_total + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(voter_total ~ candidate_total + newndp +  split_ndp +mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(voter_total ~ candidate_total + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fenegbin(voter_total ~ candidate_total + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing+ round2+round1 | governorate, data3)       


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


##### Table (11)

#Defining two variables for this analysis
data$candid_dum <- as.numeric(data$candidate_total>0)
data3$candid_dum <- as.numeric(data3$candidate_total>0)

#The analysis
main1 <- fenegbin(voter_total ~ candid_dum + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fenegbin(voter_total ~ candid_dum + newndp +  split_ndp +mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fenegbin(voter_total ~ candid_dum + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fenegbin(voter_total ~ candid_dum + newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing+ round2+round1 | governorate, data3)       


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


###### Table (12)

main1 <- fenegbin(voter_total ~ candidate_total*newndp +  candidate_total*split_ndp + candidate_total*mb_dummy +  candidate_total*pct_urban + candidate_total*pct_emp + candidate_total*sdeduscore  + candidate_total*logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

etable(main1,   se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


###### Table (13)

main1 <- fenegbin(candidate_total ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main2 <- feols(candid_dum ~ newndp +  split_ndp + mb_dummy +  pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing + round2, data)

etable(main1, main2,   se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")



##### Table (14)

main1 <- fenegbin(allintimid ~  mb_dummy*newndp +  mb_dummy*split_ndp + mb_dummy + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, data)            

etable(main1,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")

